<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm" style="padding-bottom: 500px">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <?php include "../public/substt.php";?>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>项目信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=zhi_xiang','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=zhi_xiang','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>付款信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>付款单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>款项类型</label>
                        <div class="layui-input-block">
                            <select id="feilei" name="feilei" lay-filter="feilei"></select>
                            <p class="error" id="feilei_error" style="margin: 0;"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>款项摘要</label>
                        <div class="layui-input-block">
                            <input type="text" name="zhai" id="zhai" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>付款依据</label>
                        <div class="layui-input-block">
                            <select id="yiju" name="yiju" lay-filter="yiju"></select>
                            <p class="error" id="yiju_error" style="margin: 0;"></p>
                        </div>
                    </div>
                    <div class="layui-col-md6 yujunone layui-hide">
                        <label class="layui-form-label"><text>*</text>依据名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="yijuid" class="layui-input yijuid" onclick="choose_yijuid()">
                            <div class="choose_div" id="yijuid" key="1"></div>
                            <span class="choose_one" id="yijuDivClick" key="" onclick="choose_yijuid()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>申请付款金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>支付方式</label>
                        <div class="layui-input-block">
                            <div id="incdaotype"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12 duieryuan">
                        <label class="layui-form-label"><text>*</text>收款方</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="duier" class="layui-input duier" onclick="openPage('../public/choose_dealcom.php?dom=duier&page=zhi_xiang','80%','80%')">
                            <div class="choose_div" id="duier" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_dealcom.php?dom=duier&page=zhi_xiang','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md12 duierzhan layui-hide">
                        <label class="layui-form-label"><text>*</text>收款方</label>
                        <div class="layui-input-block">
                            <input type="text" id="duierzhan" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>账户账号</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="banknum" class="layui-input banknum" onclick="choose_dealcom_bank()">
                            <div class="choose_div" id="banknum" key="1"></div>
                            <span class="choose_one" onclick="choose_dealcom_bank()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">开户行</label>
                        <div class="layui-input-block">
                            <input type="text" name="bankname" id="bankname" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>票款情况</label>
                        <div class="layui-input-block">
                            <select id="piaoqing" name="piaoqing" lay-filter="piaoqing"></select>
                            <p class="error" id="piaoqing_error" style="margin: 0;"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item piaoqing1 layui-hide height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>关联项目收票</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="piaoids" class="layui-input piaoids" onclick="choose_piao_shou_list()">
                            <div class="choose_div" id="piaoids" key="1"></div>
                            <span class="choose_one" onclick="choose_piao_shou_list()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>款项明细</span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>费用项</th>
                            <th><text>*</text>金额(元)</th>
                            <th>款项说明</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>

            <?php include '../public/flowqita.php'?>
            <?php include '../public/upinfo.php'?>
        </div>
    </div>
</form>
<?php include 'showrrboxtext.php'?>
<div><?php include '../public/shrrbox_addpage.php'?></div>
<script>
    var param_id = getQueryVariable('id') || 0;
    chu_flow_edit_yushen();


    $("#commentForm").validate({
        rules: {
            zhai: {required: true},
        },
        messages:{
            zhai: {required: "请输入"},
        }
    });

    
    function choose_dealcom_bank(obj){
        var domkey = $(obj).parents('tr').index();
        var name = get_choose_val('duier');
        if(!name){
            facade.error("请先选择收款方");return false;
        }
        openPage('../public/choose_dealcom_bank.php?page=zhi_xiang&name='+encodeURIComponent(name)+'&domkey='+domkey,'80%','80%');
    }

    function choose_piao_shou_list(obj){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目名称");return false;
        }
        openPage('../public/choose_piao_shou_list.php?choose_type=checkbox&dom=piaoids&page=zhi_xiang&enginid='+enginid,'80%','80%');
    }

    function choose_yijuid(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目名称");return false;
        }

        var feilei_text = $('select[name="feilei"] option:selected').text();
        if(!feilei_text){
            facade.error("请先选择款项类型");return false;
        }

        var feileiStr = '';
        if(feilei_text == '材料费') feileiStr = 'cai';
        if(feilei_text == '人工费') feileiStr = 'ren';
        if(feilei_text == '机械设备费') feileiStr = 'ji';
        if(feilei_text == '专业分包费') feileiStr = 'fen';
        if(feilei_text == '其他费') feileiStr = 'qi';

        var key = $("#yijuDivClick").attr('key') || '';
        if(!key){
            facade.error("请先选择付款依据");return false;
        }
        if(key == 'cai'){
            openPage('../public/choose_contract_cai.php?dom=yijuid&enginid='+enginid+'&page=zhi_xiang&caitype='+feileiStr,'80%','80%')
        }else if(key == 'caijie'){
            openPage('../public/choose_contract_cai_jie.php?dom=yijuid&enginid='+enginid+'&page=zhi_xiang&caitype='+feileiStr,'80%','80%')
        }else if(key == 'buy'){
            openPage('../public/choose_contract_buy.php?dom=yijuid&enginid='+enginid+'&page=zhi_xiang&buytype='+feileiStr,'80%','80%')
        }else if(key == 'stuffru'){
            openPage('../public/choose_stuff_ru.php?dom=yijuid&enginid='+enginid+'&page=zhi_xiang','80%','80%')
        }
    }

    var pub_feilei, pub_yiju;

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();
        facade.ajax({type: 'get', url: ask_flowdata_edit_zhi_xiang, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                chu_flow_qian(data);


                var engininfo = res.data.engininfo
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);

                    set_right_relainfo(engininfo.id, data.duier);
                }else{
                    set_right_relainfo(0, data.duier);
                }

                $("#number").val(data.number);
                $("#zhai").val(data.zhai);
                $("#money").val(data.money);
                $("#explain").val(data.explain);

                xmselect_dan('incdaotype', res.data.incdaotype, data.incdaotype, function (res) {
                    if(res.arr && res.arr.length > 0){
                        $("#incdaotype").find('xm-select').removeClass('error');
                    }else{
                        $("#incdaotype").find('xm-select').addClass('error');
                    }
                });

                xiala_zhan_two('feilei', res.data.feilei, data.feilei, 'id', 'name');
                xiala_zhan_one_zuo('yiju', res.data.yiju, data.yiju);
                xiala_zhan_one_zuo('piaoqing', res.data.piaoqing, data.piaoqing);

                if(data.duier){
                    set_choose_btn('duier',data.duier,data.duier);
                    $("#duierzhan").val(data.duier);
                }
                if(data.banknum){
                    set_choose_btn('banknum',data.banknum,data.banknum);
                    $("#bankname").val(data.bankname);
                }

                if(data.yiju && data.yiju != 'none'){
                    $(".yujunone").removeClass('layui-hide');
                    $(".duieryuan").addClass('layui-hide');
                    $(".duierzhan").removeClass('layui-hide');
                    var yijuidinfo = res.data.yijuidinfo;
                    if(yijuidinfo){
                        set_choose_btn('yijuid',data.yijuid,yijuidinfo.name);
                    }
                }

                if(data.piaoqing == 1){
                    $(".piaoqing1").removeClass('layui-hide');
                    var piaoidsinfo = res.data.piaoidsinfo;
                    if(piaoidsinfo){
                        for (var key in piaoidsinfo){
                            set_choose_btn('piaoids',piaoidsinfo[key].id,piaoidsinfo[key].nums);
                        }

                        setTimeout(function () {
                            var h = $("#piaoids").height();
                            $(".piaoids" ).css('cssText','height:'+ (h+21) +'px !important');
                        },100)
                    }
                }








                pub_yiju = res.data.yiju;
                pub_feilei = res.data.feilei;

                var feitype = [];
                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><select class="layui-select feitype">';
                listAddStr += '<option value="">请选择</option>';
                for (var mmm in feitype) {
                    listAddStr += '<option value="'+feitype[mmm].id+'">'+feitype[mmm].name+'</option>';
                }
                listAddStr += '</select></td>';
                listAddStr += '<td><input type="number" class="layui-input money"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1, {}, 'set_key1_add_str');


                var list = res.data.list;
                if(list.length > 0){
                    var feitype = [];
                    var feilei = data.feilei || 0;
                    for (var mmm in pub_feilei){
                        if(pub_feilei[mmm].id == feilei){
                            feitype = pub_feilei[mmm].children;
                        }
                    }

                    for (var key in list){
                        var str = '';
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><select class="layui-select feitype">';
                        str += '<option value="">请选择</option>';
                        for (var mmm in feitype) {
                            if(feitype[mmm].id == list[key].feitype){
                                str += '<option value="'+feitype[mmm].id+'" selected>'+feitype[mmm].name+'</option>';
                            }else{
                                str += '<option value="'+feitype[mmm].id+'">'+feitype[mmm].name+'</option>';
                            }
                        }
                        str += '</select></td>';
                        str += '<td><input type="number" class="layui-input money" value="'+list[key].money+'"></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                        $("#list_list").append(str);
                    }
                }
                update_refresh_row_func()


                initParseMoney();
                public_upload_file_load('upload_files', res.data.files);
                render_from();
            }
        });

        form.on('select(feilei)', function(data){
            var feitype = [];
            var feilei = data.value || 0;
            for (var mmm in pub_feilei){
                if(pub_feilei[mmm].id == feilei){
                    feitype = pub_feilei[mmm].children;
                }
            }

            $.each($("#list_list").children('tr'),function (key,val) {
                var seleStr = '';
                seleStr += '<option value="">请选择</option>';
                for (var mmm in feitype) {
                    seleStr += '<option value="'+feitype[mmm].id+'">'+feitype[mmm].name+'</option>';
                }
                $(this).find('.feitype').html(seleStr);
            });


            set_yiju(data.elem[data.elem.selectedIndex].text);
            render_from();

            if(data.value == ''){
                $('#feilei_error').show().text('请选择');
                $('#feilei_error').parent().find('input').addClass('select_error');
            }else{
                $('#feilei_error').hide();
                $('#feilei_error').parent().find('input').removeClass('select_error');
            }
        });

        form.on('select(yiju)', function(data){
            if(data.value == '' || data.value == 'none'){
                $(".yujunone").addClass('layui-hide');
            }else{
                $(".yujunone").removeClass('layui-hide');
            }

            clear_qing_data();
            $("#yijuDivClick").attr('key', data.value);

            if(data.value == ''){
                $('#yiju_error').show().text('请选择');
                $('#yiju_error').parent().find('input').addClass('select_error');
            }else{
                $('#yiju_error').hide();
                $('#yiju_error').parent().find('input').removeClass('select_error');
            }
        });

        form.on('select(piaoqing)', function(data){
            if(data.value == 1){
                $(".piaoqing1").removeClass('layui-hide');
            }else{
                $(".piaoqing1").addClass('layui-hide');
            }

            if(data.value == ''){
                $('#piaoqing_error').show().text('请选择');
                $('#piaoqing_error').parent().find('input').addClass('select_error');
            }else{
                $('#piaoqing_error').hide();
                $('#piaoqing_error').parent().find('input').removeClass('select_error');
            }
        });

        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.isshen = $(this).attr('isshen') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            parseMoney(data);

            data.field.enginid = get_choose_val('enginid');
            data.field.yijuid = get_choose_val('yijuid');
            data.field.duier = get_choose_val('duier');
            data.field.banknum = get_choose_val('banknum');
            data.field.piaoids = get_choose_val('piaoids');
            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();
                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }

                if(data.field.feilei == ''){
                    $('#feilei_error').show().text('请选择');
                    $('#feilei_error').parent().find('input').addClass('select_error');
                    validRes = false;
                }else{
                    $('#feilei_error').hide();
                    $('#feilei_error').parent().find('input').removeClass('select_error');
                }

                if(data.field.yiju == ''){
                    $('#yiju_error').show().text('请选择');
                    $('#yiju_error').parent().find('input').addClass('select_error');
                    validRes = false;
                }else{
                    $('#yiju_error').hide();
                    $('#yiju_error').parent().find('input').removeClass('select_error');
                }

                if(data.field.yiju != 'none'){
                    formValid('yijuid');
                    if(data.field.yijuid == undefined || !data.field.yijuid){
                        validRes = false;
                    }
                }

                if(!data.field.incdaotype){
                    $("#incdaotype").find('xm-select').addClass('error');
                    validRes = false;
                }else{
                    $("#incdaotype").find('xm-select').removeClass('error');
                }

                formValid('duier');
                if(data.field.duier == undefined || !data.field.duier){
                    validRes = false;
                }

                formValid('banknum');
                if(data.field.banknum == undefined || !data.field.banknum){
                    validRes = false;
                }

                if(data.field.piaoqing == ''){
                    $('#piaoqing_error').show().text('请选择');
                    $('#piaoqing_error').parent().find('input').addClass('select_error');
                    validRes = false;
                }else{
                    $('#piaoqing_error').hide();
                    $('#piaoqing_error').parent().find('input').removeClass('select_error');
                }

                if(data.field.piaoqing == 1){
                    formValid('piaoqing');
                    if(data.field.piaoqing == undefined || !data.field.piaoqing){
                        validRes = false;
                    }
                }

                if(!validRes){return false;}
            }


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var feitype = $(this).find('.feitype').val();
                    var money = $(this).find('.money').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(data.field.isturn > 0){
                        if(!feitype){
                            throw new Error('第'+index+'行，请选择费用项');
                        }
                        if(money==''){
                            throw new Error('第'+index+'行，请输入金额');
                        }
                    }

                    list.push({
                        feitype:feitype,
                        money:money,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);

            public_flow_submit(data.field, ask_flowdata_add_zhi_xiang, ask_flowdata_edit_zhi_xiang, $(this).attr('but'));
            return false;
        });

    });




    $('#list_list').on('input propertychange','.money',function () {
        jisuan_money_all();
    });
    function jisuan_money_all() {
        var allmoney = 0;
        $('#list_list tr').each(function (i) {
            var money = parseFloat($(this).find('.money').val()) || 0;
            allmoney = parseFloat(allmoney) + parseFloat(money);
        });
        $("#money").val(allmoney);
        initParseMoney();
    }

    function update_refresh_row_func() {
        render_date();
        jisuan_money_all();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }





    function set_yiju(value) {
        var temp_yiju = {};
        for (var key in pub_yiju){
            if(key == 'stuffru' ){
                if(value == '材料费' ){
                    temp_yiju[key] = pub_yiju[key];
                }
            }else{
                temp_yiju[key] = pub_yiju[key];
            }
        }
        xiala_zhan_one_zuo('yiju', temp_yiju, '');
        clear_qing_data();
        render_from();
    }
    function clear_qing_data() {
        var yiju = $("#yiju").val();
        if(yiju && yiju != 'none'){
            $(".yujunone").removeClass('layui-hide');
            $(".duieryuan").addClass('layui-hide');
            $(".duierzhan").removeClass('layui-hide');
        }else{
            $(".yujunone").addClass('layui-hide');
            $(".duieryuan").removeClass('layui-hide');
            $(".duierzhan").addClass('layui-hide');
        }

        $("#yijuid").html('');
        $("#yijuDivClick").attr('key', '')

        $("#duier").html('');
        $("#duierzhan").val('');
        $("#banknum").html('');
        $("#bankname").val('');
    }

    function set_key1_add_str() {
        var feitype = [];
        var feilei = $("#feilei").val() || 0;
        for (var mmm in pub_feilei){
            if(pub_feilei[mmm].id == feilei){
                feitype = pub_feilei[mmm].children;
            }
        }

        var listAddStr = '';
        listAddStr += '<tr>';
        listAddStr += '<td style="width: 45px">1</td>';
        listAddStr += '<td><select class="layui-select feitype">';
        listAddStr += '<option value="">请选择</option>';
        for (var mmm in feitype) {
            listAddStr += '<option value="'+feitype[mmm].id+'">'+feitype[mmm].name+'</option>';
        }
        listAddStr += '</select></td>';
        listAddStr += '<td><input type="number" class="layui-input money"></td>';
        listAddStr += '<td><input type="text" class="layui-input explain"></td>';
        listAddStr += '</tr>';
        return listAddStr;
    }
</script>
</body>
</html>
